#include "devices.h"
#include "idcard/idcard.h"
#include <fstream>

using namespace std;

int main()
{
    char buf[512];

    const char *info = idcard::getInfo();
    printf("%s\n", info);

    idcard::init();

    int n = idcard::getIDCardData(0, buf, 512);
    if (0 != n)
        printf ("Error: %d\n", n);
    else
        printf("%s\n", buf);

    idcard::getIDCardData(1, buf, 64);
    printf("姓名: %s\n", buf);

    idcard::getIDCardData(2, buf, 16);
    printf("性别: %s\n", buf);

    idcard::getIDCardData(3, buf, 128);
    printf("民族: %s\n", buf);

    idcard::getIDCardData(4, buf, 16);
    printf("出生年: %s\n", buf);

    idcard::getIDCardData(5, buf, 16);
    printf("出生月: %s\n", buf);

    idcard::getIDCardData(6, buf, 16);
    printf("出生日: %s\n", buf);

    idcard::getIDCardData(7, buf, 256);
    printf("住址: %s\n", buf);

    idcard::getIDCardData(8, buf, 64);
    printf("公民身份号码: %s\n", buf);

    idcard::getIDCardData(9, buf, 256);
    printf("签发机关: %s\n", buf);

    idcard::getIDCardData(10, buf, 16);
    printf("起始年: %s\n", buf);

    idcard::getIDCardData(11, buf, 16);
    printf("起始月: %s\n", buf);

    idcard::getIDCardData(12, buf, 16);
    printf("起始日: %s\n", buf);

    idcard::getIDCardData(13, buf, 16);
    printf("截止年: %s\n", buf);

    idcard::getIDCardData(14, buf, 16);
    printf("截止月: %s\n", buf);

    idcard::getIDCardData(15, buf, 16);
    printf("截止日: %s\n", buf);

    idcard::getIDCardData(16, buf, 64);
    printf("芯片序列号: %s\n", buf);

    const char *fpath = "/tmp/id.bmp.b64";
    char *buff = nullptr;
    int len = 0;
    n = idcard::getIDCardImages(1, &buff, len);
    ofstream out(fpath, ios::out);

    if (out.is_open()) {
        out << buff << endl;
        out.close();
    }
    delete [] buff;
    printf("base64 data in file %s , lenngth: %d\n", fpath, len);

    return 0;
}
